package com.qf.springmvc02.exception;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 钻五大人
 * @date 2022/5/5 10:32
 */
@Component // 声明组件
public class CustomerExceptionResolver implements HandlerExceptionResolver {

    // 返回的ModelAndView 可以指定显式异常信息的页面 也可以设置异常信息
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        // 只抛出去自定义的异常对象
        CustomerException exception;
        if (e instanceof CustomerException){
            exception = (CustomerException) e;
        }else{
            exception = new CustomerException(e.getMessage());
        }
        ModelAndView modelAndView = new ModelAndView("error");
        modelAndView.addObject("error",exception.getMessage());
        return modelAndView;
    }
}
